<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue"
import { reactive, ref } from "vue";
import useUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router'
import { ElNotification } from "element-plus";
let userStore = useUserStore()
let $router = useRouter()
let loginFrom = reactive({ username: "admin", password: "123456" })
let loading = ref(false)
const login = async () => {
  loading.value = true
  try {
    await userStore.userLogin(loginFrom)
    $router.push('/')
    ElNotification({
      type: 'success',
      message: "登录成功",
      title: "登录成功"
    })
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message
    })
  } finally {
    loading.value = false;
  }

}
const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
  ]
}
</script>

<template>
  <div class="login_container">
    <el-form class="login_form" :model="loginFrom" :rules="rules">
      <div class="title_txt">
        <h1>登录</h1>
      </div>
      <el-form-item prop="username">
        <el-input :prefix-icon="User" v-model="loginFrom.username" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" :prefix-icon="Lock" show-password v-model="loginFrom.password" />
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" class="login_sub" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="scss">
$bg: #283443;

.login_container {
  height: 100vh;
  width: 100%;
  background-color: $bg;

  .login_form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 16px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    top: 30vh;

    .login_sub {
      width: 100%;
      margin-bottom: 30px;
      height: 43px;
      font-size: 18px;

    }

    .el-input {
      margin-bottom: 15px;
      height: 50px;
      // font-size: 18px;
    }


    .title_txt {
      text-align: center;
      margin-bottom: 30px;

      h1 {
        color: white;
        font-size: 38px;
        font-weight: bold;
      }
    }
  }
}

:deep(.el-icon) {
  font-size: 20px;
}
</style>